package com.wanna.common.doc.scanner;

import com.wanna.common.doc.entity.ApiInfo;
import com.wanna.common.doc.entity.ScannerConfig;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 接口扫描器
 *
 * @author wanna
 * @since 2018-11-25
 */
public interface ApiScanner {

    /**
     * 扫描包下所有的类,并返回 api 对象
     *
     * @param scannerConfig 扫描配置
     * @param packageName   扫描的包路径
     * @return interfaces interfaces
     * @throws Exception 扫描失败
     */
    List<ApiInfo> scan(ScannerConfig scannerConfig, String packageName) throws Exception;


    /**
     * 扫描多个包路径
     *
     * @param scannerConfig 扫描配置
     * @param packageNames  扫描包路径
     * @return interfaces
     */
    default List<ApiInfo> scan(ScannerConfig scannerConfig, String... packageNames) {
        return Stream.of(packageNames)
                .map(packagePath -> scanner(scannerConfig, packagePath))
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
    }

    /**
     * scanner without exception
     *
     * @param scannerConfig 扫描配置
     * @param packageName   package
     * @return interfaces result
     */
    default List<ApiInfo> scanner(ScannerConfig scannerConfig, String packageName) {
        try {
            return scan(scannerConfig, packageName);
        } catch (Exception e) {
            return Collections.emptyList();
        }
    }
}
